package thomas.springmvc.ajax.controller;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import thomas.springmvc.ajax.constants.SystemHWUtil;
import thomas.springmvc.ajax.pojo.Book;

@Controller
public class BookController {
	
	private Log LOG = LogFactory.getLog(BookController.class);
	
	@RequestMapping("/toBook")
	public String toBook(){
		return "book";
	}
	
	@PostMapping(value= "/book", produces= SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)//"application/json;charset=UTF-8"
	@ResponseBody
	public String book(@RequestBody Book book) throws JsonProcessingException{
		LOG.info(book);
		ObjectMapper mapper = new ObjectMapper();
		String response = mapper.writeValueAsString(book);
		LOG.info(response);
		return response;
	}
}
